package com.wz.util;

import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;


public class PojoValidator {
    //获取对象
    private  static Validator validator= Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

    public static <T> boolean  validate (T pojo){
        Set<ConstraintViolation<T>>  validateResult=validator.validate(pojo);//存储错误信息的
        if(validateResult.size()>0){  //判断错误信息是否存在 大于零 即存在
            System.out.println(validateResult.iterator().next().getMessage()+"校验失败");//打印错误信息
            return  false;
        }
        return  true;  //小于零全通过
    }
}
